package com.test.chatrobot3.controller;

import com.test.chatrobot3.entity.ChatRequest;
import com.test.chatrobot3.entity.ChatResponse;
import com.test.chatrobot3.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;


    @PostMapping("/conversation")
    public ResponseEntity<ChatResponse> handleConversation(@RequestBody ChatRequest request) {
        try {
            // 调用 ChatService 处理消息
            System.out.println(request.getSessionId());
            String response = chatService.processMessage(request.getSessionId(), request.getMessage());
            System.out.println(response);
            // 返回成功响应
            return ResponseEntity.ok(new ChatResponse(response));
        } catch (Exception e) {
            // 返回错误响应
            return new ResponseEntity<>(new ChatResponse("服务暂时不可用，请稍后再试"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @PostMapping("/reset")
    public ResponseEntity<ChatResponse> resetConversation(@RequestParam String sessionId) {
        try {
            // 调用 ChatService 重置会话上下文
            chatService.resetSessionContext(sessionId);

            // 返回成功响应
            return ResponseEntity.ok(new ChatResponse("会话已重置"));
        } catch (Exception e) {
            // 返回错误响应
            return new ResponseEntity<>(new ChatResponse("服务暂时不可用，请稍后再试"), HttpStatus.INTERNAL_SERVER_ERROR);
//                    ResponseEntity.internalServerError().body(new ChatResponse("重置会话失败"));
        }
    }


}